package cn.laixueit.service.impl;

import cn.laixueit.common.Utils;
import cn.laixueit.mapper.GoodsDetailMapper;
import cn.laixueit.mapper.GoodsMapper;
import cn.laixueit.pojo.Goods;
import cn.laixueit.pojo.GoodsDetail;
import cn.laixueit.pojo.GoodsExample;
import cn.laixueit.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsDetailMapper goodsDetailMapper;

    @Transactional(rollbackFor = RuntimeException.class)
    public void save(Goods goods) {
        Long goodsId = Utils.hashId();
        goods.setId(goodsId);
        goods.setCreatetime(new Date());
        goods.setLasttime(new Date());
        goods.setSales(0);
        goods.setStatus(0);
        goodsMapper.insertSelective(goods);

        GoodsDetail goodsDetail = (GoodsDetail) goods;
        goodsDetail.setGid(goodsId);
        goodsDetailMapper.insertSelective(goodsDetail);
    }

    @Override
    public PageInfo<Goods> listPage(Integer startPage, Integer pageSize,String sortby, Goods goods) {
        GoodsExample example = getGoodsExample(goods);
        if(StringUtils.isNotBlank(sortby)){
            example.setOrderByClause(sortby);
        }
        PageHelper.startPage(startPage,pageSize);
        List<Goods> goodsList = goodsMapper.selectByExample(example);
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
        return pageInfo;
    }

    @Override
    public Goods findGoodsById(Long id) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        GoodsDetail goodsDetail = goodsDetailMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(goodsDetail,goods);
        return goods;
    }

    @Override
    @Transactional
    public void update(Goods goods) {
        goodsMapper.updateByPrimaryKey(goods);
        goodsDetailMapper.updateByPrimaryKeyWithBLOBs(goods);
    }

    @Override
    public void delById(Long id) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        GoodsDetail goodsDetail = goodsDetailMapper.selectByPrimaryKey(id);
        if(goods!=null && goodsDetail!=null){
            goods.setStatus(0);
            goodsMapper.updateByPrimaryKey(goods);
        }
    }


    private GoodsExample getGoodsExample(Goods goods){
        GoodsExample goodsExample = new GoodsExample();
        if(goods!=null){
            if(goods.getSales()==null) {
                goodsExample.setOrderByClause("createtime desc");
            }else{
                goodsExample.setOrderByClause("sales desc");
            }
            GoodsExample.Criteria criteria = goodsExample.createCriteria();
            if(StringUtils.isNotBlank(goods.getName())){
                criteria.andNameLike("%"+goods.getName()+"%");
            }
            if(goods.getCreatetime() !=null){
                criteria.andCreatetimeGreaterThanOrEqualTo(goods.getCreatetime());
            }
            if(goods.getStatus()!=null){
                criteria.andStatusEqualTo(goods.getStatus());
            }
            if(goods.getCategory3()!=null){
                criteria.andCategory3EqualTo(goods.getCategory3());
            }
            if(goods.getBrandid()!=null && goods.getBrandid()!=0){
                criteria.andBrandidEqualTo(goods.getBrandid());
            }
            if(!CollectionUtils.isEmpty(goods.getMapPrice()) && goods.getMapPrice().size()>0 ){
                Map<String, Integer> mapPrice = goods.getMapPrice();

                Integer startPrice = mapPrice.get("startPrice");
                startPrice = startPrice==null ?0:startPrice;

                if(mapPrice.get("endPrice")==null)  {
                    criteria.andPriceGreaterThanOrEqualTo( startPrice* 100);
                }else {
                    criteria.andPriceBetween(startPrice*100, mapPrice.get("endPrice") * 100);
                }
            }
        }
        return goodsExample;
    }
}
